package com.syb.controller.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.syb.entity.user.SysUser;
import com.syb.service.UserService;
import com.syb.util.PageResults;

@Namespace("/user")
public class UserController extends ActionSupport{

	Logger logger = Logger.getLogger(UserController.class);
	
	@Autowired
	private UserService userService;
	
	private Integer currentPage;
	private Integer id;
	private String sysName;
	private String sysPassword;
	private Integer sysSex;
	private String sysPhone;
	private String sysAddress;
	private String ids;
	
	@Action(value="getUserList",
			results={
			@Result(name="success",location="/WEB-INF/page/user/userList.jsp"),
			@Result(name="error",location="/WEB-INF/fail.jsp")
			})
	public String getUserList(){
		PageResults<SysUser> page = new PageResults<SysUser>();
		PageResults<SysUser> sysUserList = new PageResults<SysUser>();
		//组装参数
		if(currentPage!=null&&!currentPage.equals("")){
			page.setCurrentPage(currentPage);
		}
		try{
			sysUserList = userService.getUserList(page);
		}catch(Exception e){
			logger.error(e.getMessage());
			ActionContext.getContext().put("error","查询出错！");
			return "error";
		}
		ActionContext.getContext().put("result", sysUserList);
		return "success";
	}
	
	@Action(value="saveUser",
			results={
			@Result(name="success",type="redirect",location="/user/getUserList"),
			@Result(name="error",location="/WEB-INF/page/user/add.jsp")
			})
	public String saveUser(){
		SysUser sys = new SysUser();
		sys.setSysAddress(sysAddress);
		sys.setSysName(sysName);
		sys.setSysPassword(sysPassword);
		sys.setSysPhone(sysPhone);
		sys.setSysSex(sysSex);
		try{
			userService.saveSysUser(sys);
		}catch(Exception e){
			logger.error(e.getMessage());
			ActionContext.getContext().put("result",sys);
			ActionContext.getContext().put("error",e.getMessage());
			return "error";
		}
		return "success";
	}
	
	//添加跳转页面
	@Action(value="tzSaveUser",
			results={
			@Result(name="success",location="/WEB-INF/page/user/add.jsp"),
			@Result(name="error",location="/WEB-INF/fail.jsp")
			})
	public String tzSaveUser(){
		return "success";
	}
	
	//修改跳转页面
	@Action(value="tzUpdateUser",
			results={
			@Result(name="success",location="/WEB-INF/page/user/update.jsp"),
			@Result(name="error",location="/WEB-INF/fail.jsp")
			})
	public String tzUpdateUser(){
		SysUser sys = new SysUser();
		try{
			sys = userService.tzUpdateUser(id);
		}catch(Exception e){
			logger.error(e.getMessage());
			ActionContext.getContext().put("error","查询出错！");
			return "error";
		}
		ActionContext.getContext().put("result",sys);
		return "success";
	}

	
		//分配角色跳转页面
		@Action(value="fpRoleList",
				results={
				@Result(name="success",location="/WEB-INF/page/user/roleList.jsp"),
				@Result(name="error",location="/WEB-INF/fail.jsp")
				})
		public String fpRoleList(){
			List<Map<String,Object>> map = new ArrayList<Map<String,Object>>();
			try{
				map = userService.findRoleList(id);
			}catch(Exception e){
				logger.error(e.getMessage());
				ActionContext.getContext().put("error","查询出错！");
				return "error";
			}
			ActionContext.getContext().put("result",map);
			return "success";
		}
	
	
	@Action(value="updateUser",
			results={
			@Result(name="success",type="redirect",location="/user/getUserList"),
			@Result(name="error",location="/WEB-INF/fail.jsp")
			})
	public String updateUser(){
		SysUser sys = new SysUser();
		sys.setId(id);
		sys.setSysAddress(sysAddress);
		sys.setSysName(sysName);
		sys.setSysPassword(sysPassword);
		sys.setSysPhone(sysPhone);
		sys.setSysSex(sysSex);
		try{
			userService.updateSysUser(sys);
		}catch(Exception e){
			logger.error(e.getMessage());
			ActionContext.getContext().put("error","查询出错！");
			return "error";
		}
		return "success";
	}

	
	
		//更新用户所拥有的角色
		@Action(value="updateRoleList",
				results={
				@Result(name="success",type="redirect",location="/user/getUserList"),
				@Result(name="error",location="/WEB-INF/fail.jsp")
				})
		public String updateRoleList(){
			 List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
			 Map<String,Object> map = null;
			if(ids.length()>0){
			   ids = ids.substring(0, ids.length()-1);
			  String idss[] = ids.split(",");
			  for(int i=0;i<idss.length;i++){
				  map = new HashMap<String,Object>();
				  map.put("roleId", idss[i]);
				  map.put("id", id);
				  list.add(map);
			  }
			}else{
				map = new HashMap<String,Object>();
				map.put("id", id);
				list.add(map);
			}
			try{
				userService.updateRoleList(list);
			}catch(Exception e){
				logger.error(e.getMessage());
				return "error";
			}
			
			return "success";
		}
		
		
	
	@Action(value="deleteUser",
			results={
			@Result(name="success",type="redirect",location="/user/getUserList"),
			@Result(name="error",location="/WEB-INF/fail.jsp")
			})
	public String deleteUser(){
		SysUser sys = new SysUser();
		sys.setId(id);
		try{
			userService.deleteSysUser(sys);
		}catch(Exception e){
			logger.error(e.getMessage());
			ActionContext.getContext().put("error","查询出错！");
			return "error";
		}
		return "success";
	}
	
	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getSysName() {
		return sysName;
	}

	public void setSysName(String sysName) {
		this.sysName = sysName;
	}

	public String getSysPassword() {
		return sysPassword;
	}

	public void setSysPassword(String sysPassword) {
		this.sysPassword = sysPassword;
	}

	public Integer getSysSex() {
		return sysSex;
	}

	public void setSysSex(Integer sysSex) {
		this.sysSex = sysSex;
	}

	public String getSysPhone() {
		return sysPhone;
	}

	public void setSysPhone(String sysPhone) {
		this.sysPhone = sysPhone;
	}

	public String getSysAddress() {
		return sysAddress;
	}

	public void setSysAddress(String sysAddress) {
		this.sysAddress = sysAddress;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
	
	
}
